package maMetrics;

import maConfigration.MetricSettings;
import maUtil.LogFile;
import maUtil.RecordUnit;
import maUtil.SortedRecords;

import com.telelogic.rhapsody.core.IRPAttribute;
import com.telelogic.rhapsody.core.IRPClass;
import com.telelogic.rhapsody.core.IRPCollection;
import com.telelogic.rhapsody.core.IRPInterfaceItem;
import com.telelogic.rhapsody.core.IRPOperation;
import com.telelogic.rhapsody.core.IRPPackage;
import com.telelogic.rhapsody.core.IRPProject;

public class MetricNOC extends MetricObject {
	
	SortedRecords _srProj = null;
	SortedRecords _srPkg = null;
	
	LogFile _logFile = null;
	int _sSum;

	//csv variables
	LogFile _csvLogFileProj = null;
	SortedRecords _srProjCsv = null;
	protected LogFile _logRawData = null;
	
//	public MetricNOC() {
//		// TODO get infor from configure file
//		_sMetricName = "Metric: Number Of Children";
//		
//		_isApplyToProj = true;
//		_isApplyToCls = true;
//		
//		_logFile = new LogFile("MetricNOC.txt");
//		_sSum = 0;
//		_iWeight = 1;
//	}
	
	public MetricNOC (MetricSettings ms)
	{
		super(ms);
		_sMetricName = "Metric: Number Of Children";
		
		if(_isApplyToCls)
		{
			_srPkg = new SortedRecords(2000, _sMetricName + " Sorted By Packages");
		}
		if(_isApplyToProj)
		{
			_srProj = new SortedRecords(2000, _sMetricName + " Sorted By Packages");
			_srProjCsv = new SortedRecords(10, "NOC, metric");
		}
		_logFile = new LogFile("MetricNOC.txt");
		
		if(_isLogRawData)
		{
			_logRawData = new LogFile("MetricNOCRawData.csv");
			_logRawData.logLn("Value,Classname");
		}
		
		_sSum = 0;
	}
	
	
	/*getDerivedClassifiers returns all direct descendants to a class*/
	public static int countNOC(IRPClass cls)
	{
		IRPCollection coll = cls.getDerivedClassifiers();
		return coll.getCount();
	}
	
	private void log(String str){
		_logFile.log(str);
	}
	
	@Override
	public int applyInProj(IRPProject proj) {
		// TODO Auto-generated method stub
		/*_csvLogFileProj = new LogFile("MetricNOC"+ proj.getName() +".csv");
		if(!_isApplyToProj)
			return 0;
		if(_srProjCsv.getSize() > 0)
		{
			_csvLogFileProj.logLn("NOC,Class");
			_csvLogFileProj.logPln(_srProjCsv.getAllStringCSV());
		}
		//_csvLogFileProj = new LogFile("MetricDIT"+ proj.getName().toUpperCase() +".csv");
		_srProjCsv = new SortedRecords(10, "NOC,Class");*/
		
		int n = _sSum;
		_sSum = 0;
		return n;
	}

	@Override
	public int applyInPkg(IRPPackage pkg) {
		// TODO Auto-generated method stub
		/*if(!_isApplyToPkg)
		{
			return 0;
		}
		if(_srPkg != null)
		{
			log(_srPkg.getAllString());
		}
		_srPkg = new SortedRecords(20, _sMetricName);*/
		return 0;
	}

	@Override
	public int applyInCls(IRPClass cls) {
		// TODO Auto-generated method stub
		if(_srProj == null)
		{
			_srProj = new SortedRecords(2000, _sMetricName + " " + cls.getProject().getName());
		}
		int n = countNOC(cls);
		RecordUnit ru = new RecordUnit(n, cls.getFullPathNameIn() + " project: " + cls.getProject().getName());
		//if(_isApplyToPkg && n > _srPkg.getMinValue()) 
		//	_srPkg.add(ru);
		if(_isApplyToProj && !_srProj.contains(ru) && n > 0) //_srProj.getMinValue() ) 
			_srProj.add(ru);
		if(_isApplyToProj && n >= _srProjCsv.getMinValue())	
			_srProjCsv.add(new RecordUnit(n, cls.getName()));
		_sSum += n;
		
		if(_isLogRawData && n>0) 
			_logRawData.logLn(n + "," + cls.getFullPathNameIn());
		return n;
	}

	@Override
	public int applyInItf(IRPInterfaceItem itf) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int applyInOp(IRPOperation op) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int applyInAttr(IRPAttribute attrb) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		//System.out.println(_srProj.getAllString());
		if(_srProj != null) log(_srProj.getAllString());
	}


}
